package com.ggkt.common.utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 *
 * @author ggkt
 */
public class ParameterUtil {

	private static Log log = LogFactory.getLog(ParameterUtil.class);

	/**
	 * 获取request中的参数，返回相应对象(提供给tag中使用)
	 *
	 * @param name 参数名称
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getAttribute(String name, HttpServletRequest request) {

		return (T) (request.getAttribute(name));
	}

	/**
	 * 获取session中的参数，返回相应对象
	 *
	 * @param name 参数名称
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getAttribute(String name, HttpSession session) {

		return (T) (session.getAttribute(name));
	}

	/**
	 * 获取request 整数型参数
	 *
	 * @param name
	 */
	public static Integer getIntegerParameter(String name, HttpServletRequest request) {

		Object o = request.getParameter(name);

		return o == null ? null : str2Integer(o.toString());
	}

	/**
	 * 获取request 长整数型参数
	 */
	public static Long getLongParameter(String name, HttpServletRequest request) {

		Object o = request.getParameter(name);

		return o == null ? null : str2Long(o.toString());
	}

	/**
	 * getInteger(获取request整数型参数为空设置默认值)
	 *
	 * @param name
	 * @param request
	 * @param defaultValue
	 * @return Integer
	 */
	public static Integer getInteger(String name, HttpServletRequest request, Integer defaultValue) {

		Object o = request.getParameter(name);

		return o == null ? defaultValue : str2Integer(o.toString());
	}

	/**
	 * 获取请求(request)中的参数， 返回整型;
	 *
	 * @param name    参数名称
	 * @param request
	 * @return 整型
	 */
	public static Integer getInteger(String name, HttpServletRequest request) {

		return str2Integer(getParameter(name, request));
	}

	/**
	 * getString(获取request字符串型参数为空设置默认为空字符串)
	 *
	 * @param name
	 * @param request
	 * @return String
	 */
	public static String getString(String name, HttpServletRequest request) {

		Object o = request.getParameter(name);

		return o == null ? "" : (String) o;
	}

	public static String getStringDefa(String name, HttpServletRequest request, String defa) {

		Object o = request.getParameter(name);

		return o == null ? defa : (String) o;
	}

	/**
	 * 将字符串转为整数, null转为0
	 */
	public static Integer parseInt(String str) {
		Integer in = null;
		if (StringUtils.isNotBlank(str)) {
			try {
				in = Integer.parseInt(str);
			} catch (NumberFormatException e) {
				in = 0;
			}
		}
		return in;
	}

	/**
	 * 将字符串转为整数
	 */
	public static Integer str2Integer(String str) {
		Integer in = null;
		if (StringUtils.isNotBlank(str)) {
			try {
				in = Integer.parseInt(str);
			} catch (NumberFormatException e) {
				// test log.error(str + "不能转换为整型！", e);
				log.warn(str + "不能转换为整型！");
			}
		}
		return in;
	}

	/**
	 * 将字符串转为长整数
	 */
	public static Long str2Long(String str) {
		Long in = null;
		if (StringUtils.isNotBlank(str)) {
			try {
				in = Long.parseLong(str);
			} catch (NumberFormatException e) {
				// test log.error(str + "不能转换为长整型！", e);
				log.warn(str + "不能转换为长整型！");
			}
		}
		return in;
	}

	/**
	 * 将字符串数组转为整数数组
	 */
	public static Integer[] strArray2IntegerArray(String[] strParams) {
		Integer[] params = null;
		if (strParams != null) {
			int l = strParams.length;
			params = new Integer[l];
			for (int i = 0; i < l; i++) {
				params[i] = str2Integer(strParams[i]);
			}
		}
		return params;
	}

	public String newSuccessJsonResult(String msg) {

		return newJsonResult(1, msg);
	}

	public String newJsonResult(int status, String msg) {

		return "{\"status\":" + status + ",\"message\":\"" + msg + "\"}";
	}

	/**
	 * 获取请求(request)中的参数
	 *
	 * @param request
	 */
	public static String getParameter(String name, HttpServletRequest request) {

		String str = request.getParameter(name);
		return str == null || "null".equals(str) ? null : str;
	}

	public static String getParam(String name, String url) {

		if (StringUtils.isBlank(name) || StringUtils.isBlank(url)) {
			return null;
		}

		String value = null;
		Pattern pattern = Pattern.compile("[\\?|&]" + name + "=([^(&|#)]*)[#|&|$]*");
		Matcher matcher = pattern.matcher(url);
		if (matcher.find()) {
			value = matcher.groupCount() == 1 ? matcher.group(1) : null;
		}

		return value;
	}

	public static Integer getIntegerParam(String name, String url) {

		String o = getParam(name, url);

		return o == null ? null : str2Integer(o);
	}

	public static Long getLongParam(String name, String url) {

		String o = getParam(name, url);

		return o == null ? null : str2Long(o);
	}

	public static Map<String, Object> getUrlParameters(String url) {

		String[] loU = url.split("?");
		if (loU.length <= 1) {
			return null;
		}
//"a=1&b=2&c=3"
		Map<String, Object> params = null;
		String[] loallPm = loU[1].split("&");
		for (Integer i = 0; i < loallPm.length; i++) {
			Object[] loPm = loallPm[i].split("=");
			System.out.println(loallPm[i].toString());
		}

		return params;
	}

	public static void main(String[] args) {
		List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();

		String url = "http?c=96545454&utm_campaign=13157&utm_term=37792241&utm_source=baidu&utm_medium=sem&self=1#2&bd_vid=12142858719045456390";
		String[] loU = url.split("\\?");

		String[] loallPm = loU[1].split("&");
		for (Integer i = 0; i < loallPm.length; i++) {
			Object[] pmoObjects = loallPm[i].split("=");

			Map<String, Object> map = new HashMap<String, Object>();
			map.put(pmoObjects[0].toString(), pmoObjects[1]);

			params.add(map);
			System.out.println(map.toString());
		}

//		System.out.println(getParam("utm_campaign", url));
//		System.out.println(getParam("utm_term", url));
//		System.out.println(getParam("utm_source", url));
//		System.out.println(getParam("utm_medium", url));
//		System.out.println(getParam("self", url));
//		System.out.println(getParam("bd_vid", url));
	}

}
